package com.juxin.jiguangdemo.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;

import com.juxin.jiguangdemo.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by zhangguangbin on 2018/3/21.
 */

public class CusReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String action = intent.getAction();

        if (bundle == null || action == null) {
            LogManager.e("[MyReceiver] bundle or action is null!");
            return;
        }

        LogManager.e(printBundle(bundle));

        switch (action) {
            case JPushInterface.ACTION_REGISTRATION_ID:
                LogManager.e("[MyReceiver] 推送的RegistrationId: " + bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID));
                break;

            case JPushInterface.ACTION_MESSAGE_RECEIVED:
                LogManager.e("[MyReceiver] 推送的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
                break;
            case JPushInterface.ACTION_NOTIFICATION_RECEIVED:
                LogManager.e("[MyReceiver] 推送的通知的ID: " + bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
                break;

            case JPushInterface.ACTION_NOTIFICATION_OPENED:
                LogManager.e("[MyReceiver] 用户点击打开通知: " + bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
                Intent i = new Intent(context, MainActivity.class);
                i.putExtras(bundle);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
                break;

            case JPushInterface.ACTION_RICHPUSH_CALLBACK:
                LogManager.e("[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                break;

            case JPushInterface.ACTION_CONNECTION_CHANGE:
                LogManager.e("[MyReceiver] connected state change to：" + intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
                break;

            default:
                break;
        }
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    LogManager.e("This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    LogManager.e("Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

    //send msg to HomeActivity
    private void processCustomMessage(Context context, Bundle bundle) {
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);

        msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);

        if (!(null == extras || extras.length() == 0 || extras.trim().length() == 0)) {
            try {
                JSONObject extraJson = new JSONObject(extras);
                if (extraJson.length() > 0) {
                    msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
                }
            } catch (JSONException ignored) {

            }

        }
        LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
    }

}
